// pages/Goods/goodsOrderDetail/index.js
var QRCode = require('../../../../utils/weapp-qrcode.js')
var common = require("../../../../public.js");
var qrcode;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    id: 0,
    orderDes: {},
    code: true,
    storeInfo: {},
    isLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token
        }, () => {
          that.getOrderDes(options.orderNo)
        })
      }
    })
  },
  getOrderDes(orderNo) {
    this.setData({
      isLoading: true
    })
    const that = this
    common.get(
      '/getZeroBuyByOrderNo',
      {
        userId: that.data.userInfo.id,
        token: that.data.token,
        orderNo
      },
      function (res) {
        if (res.status === 200 && res.data) {
          const createTime = that.time(res.data.createTime)
          const paymentTime = that.time(res.data.paymentTime)
          that.setData({
            orderDes: res.data,
            createTime: createTime,
            paymentTime
          })
          that.getStoreInfo(res.data.storeId)
          qrcode = new QRCode('canvas', {
            text: "feiyangstore/sendzerogoods/order_id=" + res.data.id + "&user_id: " + that.data.userInfo.id,
            width: 150,
            height: 150,
          })
        } else {
          common.showRequestErrMsg(res)
        }
      },
      function () {
        that.setData({
          isLoading: false
        })
      }
    )
  },
  getStoreInfo(storeId) {
    common.post('/store/info', {
      storeId
    },
      (res) => {
        this.setData({
          storeInfo: res.data
        })
      })
  },
  time(time) {
    if (!time) {
      return '-'
    }
    var now = new Date(time);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    if (month <= 9) month = '0' + month;
    if (day <= 9) day = '0' + day;
    if (hours <= 9) hours = '0' + hours;
    if (minutes <= 9) minutes = '0' + minutes;
    var shu = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':00';
    return shu;
  },
  look(e) {
    var that = this;
    if (that.data.orderDes.status == 2 && that.data.orderDes.postType == 2) {
      that.setData({
        code: false
      })
    } else {
      wx.showToast({
        title: '该订单不可查看二维码',
        icon: 'none',
        duration: 2000
      })
    }
  },
  close(e) {
    var that = this;
    that.setData({
      code: true
    })
  },
  gotoGoodsDetail() {
    wx.navigateTo({
      url: '/packageC/pages/Zero/Des/index?id=' + this.data.orderDes.zerobuyId + '&name=' + this.data.orderDes.goodsTitle,
    })
  }
})